[C#] How do I make hierarchy of objects from two alternating classes?
Posted
by
Millicent
on Stack Overflow
See other posts from Stack Overflow
or by Millicent
Published on 2010-12-25T12:47:26Z
Indexed on
2010/12/25
12:54 UTC
Read the original article
Hit count: 146
Here's the scenario:
I have two classes ("Page" and "Field"), that are descended from a common class ("Pield").
They represent tags in an XML file and are in the following hierarchy:
<page>
<field>
<page>
...
</page>
...
</field>
...
</page>
I.e.: Page and Field objects are in a hierarchy of alternating type (there may be more than one Page or Field to each rung of the hierarchy).
Every Field and Page object has a parent property, which points to the respective parent object of the other type. This is not a problem unless the parent-child mechanism is controlled by the base class (Pield), which is shared by the two descended classes (Page and Field).
Here is one try, that fails at the line "Pield child = new Pield(pchild, this);":
class Pield<T>
{
private T _pield_parent;
...
private void get_children() {
...
Pield<Page> child = new Pield<Page>(pchild, this);
...
}
...
}
class Page : Pield<Field> {
...
}
class Field : Pield<Page>
{
...
}
Any ideas about how to solve this elegantly?
Best,
Millicent
© Stack Overflow or respective owner